<?php
    // product cat model
    class ProductModel
    {
        public function __construct(PDO $pdo) {
            $this->pdo = $pdo;
        }
        function getAllProduct()
        {
            $sql = "SELECT * FROM set_products where ACTIVE_PRODUCT = 1";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }
        function getProductByID($id)
        {
            $sql = "SELECT * FROM set_products where ID_PRODUCT = $id LIMIT 1";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetch();
            return $result;            
        }
        function getAllSlideProductByID($id){
            $sql = "SELECT * FROM slide_product where ID_PRODUCT = $id";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;            
        }      
        function insertProduct($arrayPro)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayPro['NAME_PRODUCT'])));
            $sql = "INSERT INTO set_products (
                IDCAT_PRODUCT,
                NAME_PRODUCT,
                CODE_PRODUCT,
                NAMESEO_PRODUCT,
                URL_PRODUCT,
                DES_PRODUCT,
                KEYWORD_PRODUCT,
                PRICE_PRODUCT,
                PRICESALE_PRODUCT,
                BH_PRODUCT,
                INFO1_PRODUCT,
                COLOR_PRODUCT,
                PHOTO_PRODUCT,
                CONTENT_PRODUCT,
               	ACTIVE_PRODUCT
                ) 
                VALUES (
                :IDCAT_PRODUCT,
                :NAME_PRODUCT,
                :CODE_PRODUCT,
                :NAMESEO_PRODUCT,
                :URL_PRODUCT,
                :DES_PRODUCT,
                :KEYWORD_PRODUCT,
                :PRICE_PRODUCT,
                :PRICESALE_PRODUCT,
                :BH_PRODUCT,
                :INFO1_PRODUCT,
                :COLOR_PRODUCT,
                :PHOTO_PRODUCT,
                :CONTENT_PRODUCT,
                :ACTIVE_PRODUCT
                )";
            $sth = $this->pdo->prepare($sql);
            $sth->execute(array(
                                ':IDCAT_PRODUCT'            =>$arrayPro['IDCAT_PRODUCT'],
                                ':NAME_PRODUCT'             =>$arrayPro['NAME_PRODUCT'],
                                ':CODE_PRODUCT'             =>$arrayPro['CODE_PRODUCT'],
                                ':NAMESEO_PRODUCT'          =>$arrayPro['NAMESEO_PRODUCT'],
                                ':URL_PRODUCT'              =>$nameUrlSeo,
                                ':DES_PRODUCT'              =>$arrayPro['DES_PRODUCT'],
                                ':KEYWORD_PRODUCT'          =>$arrayPro['KEYWORD_PRODUCT'],
                                ':PRICE_PRODUCT'            =>$arrayPro['PRICE_PRODUCT'],
                                ':PRICESALE_PRODUCT'         =>$arrayPro['PRICESALE_PRODUCT'],
                                ':BH_PRODUCT'               =>$arrayPro['BH_PRODUCT'],
                                ':INFO1_PRODUCT'            =>$arrayPro['INFO1_PRODUCT'],
                                ':COLOR_PRODUCT'            =>$arrayPro['COLOR_PRODUCT'],
                                ':PHOTO_PRODUCT'            =>$arrayPro['PHOTO_PRODUCT'],
                                ':CONTENT_PRODUCT'          =>$arrayPro['CONTENT_PRODUCT'],
                                ':ACTIVE_PRODUCT'           =>isset($arrayPro['ACTIVE_PRODUCT'])?1:0,                         
                        ));
            $stmt = $this->pdo->query("SELECT LAST_INSERT_ID() FROM set_products");
            $lastId = $stmt->fetch(PDO::FETCH_NUM);
            $lastId = $lastId[0];
            if(count($arrayPro['slide_product']) > 0){
                $idPro = $lastId;
                for($i=0; $i<count($arrayPro['slide_product']); $i++) {
                    $filePath = $arrayPro['slide_product'][$i];
                    if($filePath!='')
                    {
                        $sqlslide = "INSERT INTO slide_product (
                       	    ID_PRODUCT,
                           	PHOTO_SLIDE
                            ) 
                            VALUES (
                            :ID_PRODUCT,
                            :PHOTO_SLIDE
                            )";
                        $sthsql = $this->pdo->prepare($sqlslide);
                        $sthsql->execute(array(
                                    ':ID_PRODUCT'            =>$idPro,
                                    ':PHOTO_SLIDE'           =>$filePath                       
                            ));                        
                    }
                }                
            }
            return true;  
        }
        
        function editProduct($arrayEditPro)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayEditPro['NAME_PRODUCT'])));
            $sql = "UPDATE set_products 
                    SET IDCAT_PRODUCT=?, NAME_PRODUCT=?, CODE_PRODUCT=?, NAMESEO_PRODUCT=?, URL_PRODUCT=?,DES_PRODUCT=?,
                    KEYWORD_PRODUCT=?, PRICE_PRODUCT=?, PRICESALE_PRODUCT=?, BH_PRODUCT=?, INFO1_PRODUCT=?, COLOR_PRODUCT=?,
                    PHOTO_PRODUCT=?, CONTENT_PRODUCT=?, ACTIVE_PRODUCT=?
            		WHERE ID_PRODUCT=?";
            $sth = $this->pdo->prepare($sql);
            $result = $sth->execute(array($arrayEditPro['IDCAT_PRODUCT'],$arrayEditPro['NAME_PRODUCT'],$arrayEditPro['CODE_PRODUCT'], $arrayEditPro['NAMESEO_PRODUCT'],
            $nameUrlSeo, $arrayEditPro['DES_PRODUCT'],$arrayEditPro['KEYWORD_PRODUCT'],$arrayEditPro['PRICE_PRODUCT'],
            $arrayEditPro['PRICESALE_PRODUCT'],$arrayEditPro['BH_PRODUCT'], $arrayEditPro['INFO1_PRODUCT'], $arrayEditPro['COLOR_PRODUCT'], 
            $arrayEditPro['PHOTO_PRODUCT'], $arrayEditPro['CONTENT_PRODUCT'], isset($arrayEditPro['ACTIVE_PRODUCT'])?1:0,$arrayEditPro['ID_PRODUCT']));               

            if(count($arrayEditPro['slide_product']) > 0){
                $idPro = $arrayEditPro['ID_PRODUCT'];;
                // delete all slideshow old images
                $this->deleteAllSilideShowImage($idPro);
                for($i=0; $i<count($arrayEditPro['slide_product']); $i++) {
                    $filePath = $arrayEditPro['slide_product'][$i];
                    if($filePath!='')
                    {
                        $sqlslide = "INSERT INTO slide_product (
                       	    ID_PRODUCT,
                           	PHOTO_SLIDE
                            ) 
                            VALUES (
                            :ID_PRODUCT,
                            :PHOTO_SLIDE
                            )";
                        $sthsql = $this->pdo->prepare($sqlslide);
                        $sthsql->execute(array(
                                    ':ID_PRODUCT'            =>$idPro,
                                    ':PHOTO_SLIDE'           =>$filePath                       
                            ));                        
                    }
                }                
            }
            return true;  
        }
        function deleteAllSilideShowImage($id){
            $sql = "DELETE FROM `slide_product` WHERE `ID_PRODUCT` = $id";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();           
        }
        function deleteProduct($idPro){
            // delete all slideshow images
            $this->deleteAllSilideShowImage($idPro);
            // del product
            $sqlPro = "DELETE FROM `set_products` WHERE `ID_PRODUCT` = $idPro";
            $sthPro = $this->pdo->prepare($sqlPro);
            $sthPro->execute(); 
        }                       
    }